var Comment = require('../models/comment');

// comment
exports.save = function(req, res) {
	var _comment = req.body.comment;
	var movieId = _comment.movie;
	// 用户回复处理
	if(_comment.cid) {
		Comment.findById(_comment.cid, function(err, comment) {
			var reply = {
				from: _comment.from,
				to: _comment.tid,
				content: _comment.content
			};
			comment.replys.push(reply);

			comment.save(function(err, comment) {
				if(err) {
					console.log(err);
				}
				res.redirect('/movie/' + movieId);
			});
		});
	} else { // 用户不回复，评论处理
		var comment = new Comment(_comment);
		
		comment.save(function(err, comment) {
			if(err) {
				console.log(err); 
			}
			res.redirect('/movie/' + movieId);
		});
	}

}